package com.friend.chat.page.view.register

import android.app.Activity
import android.content.Intent
import android.util.Log
import com.friend.chat.R
import com.friend.chat.base.BaseActivity
import com.friend.chat.databinding.ActivityRegisterBinding
import com.friend.chat.page.vm.RegisterViewModel
import com.friend.chat.utils.GlideEngine
import com.friend.chat.utils.PictureSelectorHelper

class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {

    private var sex: Int = 0
    private var header: String = ""

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, RegisterActivity::class.java)
            activity.startActivity(intent)
        }
    }


    override fun yourViewModel(): RegisterViewModel {
        return RegisterViewModel()
    }

    override fun yourViewBing(): ActivityRegisterBinding {
        return ActivityRegisterBinding.inflate(layoutInflater)
    }

    override fun init() {
        mViewBinding.rb1.isChecked = true

    }

    override fun setClick() {
        super.setClick()
        mViewBinding.ivRegisterHeader.setOnClickListener {
            PictureSelectorHelper().takeAndSelectPhoto(this, 1) {
                mVModel.uploadHeader(it)
            }
        }
        mViewBinding.rgRegister.setOnCheckedChangeListener { _, id ->
            if (id == R.id.rb_1) {
                sex = 0
            } else {
                sex = 1
            }
        }
        mViewBinding.btnRegister.setOnClickListener {
            Log.e("--->","hahaha")
            val username = mViewBinding.etUsername.text.toString().trim()
            if (username.isEmpty()) {
                mVModel.toast("请输入用户名")
                return@setOnClickListener
            }
            val nickname = mViewBinding.etNickName.text.toString().trim()
            if (nickname.isEmpty()) {
                mVModel.toast("请输入昵称")
                return@setOnClickListener
            }
            val password = mViewBinding.etPassword.text.toString().trim()
            if (password.isEmpty()) {
                mVModel.toast("请输入密码")
                return@setOnClickListener
            }

            val age = mViewBinding.etAge.text.toString().trim()
            if (age.isEmpty()) {
                mVModel.toast("请输入年龄")
                return@setOnClickListener
            }

            val phone = mViewBinding.etPhone.text.toString().trim()
            if (phone.isEmpty()) {
                mVModel.toast("请输入手机号")
            }

            mVModel.register(
                username,
                nickname = nickname,
                password = password,
                phone = phone,
                header = header,
                sex = sex,
                age  = age.toInt()
            )
        }
    }

    override fun onNetListener() {
        super.onNetListener()
        mVModel.uploadPicResponse.observe(this) {
            val data = it.data ?: return@observe
            header = data
            GlideEngine.createGlideEngine()
                .loadImage(this, header, mViewBinding.ivRegisterHeader)
        }
        mVModel.registerResponse.observe(this){
            val data = it.data?:return@observe
            if (data){
                mVModel.toast("注册成功")
                LoginActivity.open(this)
                finish()
            }else{
                mVModel.toast("注册失败")
            }
        }
    }
}